/**
 * 项目名：	Rumba Plan
 * 
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2006，所有权利保留。
 * 
 * 文件名：	OrganizationFilter.java
 * 模块说明：	
 * 修改历史：
 * 2006-7-7 - lxm - 创建。
 */
package com.hd123.rumba.action.organization;

import com.hd123.rumba.organization.COrganization;
import com.hd123.rumba.organization.biz.QOrganization;
import com.hd123.rumba.util.HashMap2;

/**
 * 组织筛选条件 | 界面数据对象
 * 
 * @author lxm
 * 
 */
public class OrganizationFilter {

  /** 代码类似于 */
  private String codeLike = null;
  /** 名称类似于 */
  private String nameLike = null;
  /** 上级组织。[NORMAL form only] */
  private QOrganization upperOrg = null;
  /** 排除组织。[NORMAL form only] */
  private QOrganization exceptOrg = null;
  /** 状态。[NORMAL form only] */
  private Integer state = VALUE_STATE_VALIDATE;
  public static final Integer VALUE_STATE_VALIDATE = new Integer(0);
  public static final String CAPTION_STATE_VALIDATE = "仅可用的";
  public static final Integer VALUE_STATE_ALL = new Integer(1);
  public static final String CAPTION_STATE_ALL = "全部";
  public static final HashMap2 STATEMAP = new HashMap2().put2(VALUE_STATE_VALIDATE,
      CAPTION_STATE_VALIDATE).put2(VALUE_STATE_ALL, CAPTION_STATE_ALL);

  /**
   * 输出查询条件。输出格式为javascript（RQuery控件）。
   * 
   * @param qryName
   *          RQuery控件javascript变量名。
   * @param alias
   *          组织表对应的别名。
   * @param isRecycle
   *          是否是回收站视图。
   * @return 以字符串形式返回相应的javascript语句。
   */
  public String toJSForRQuery(String qryName, String alias, Boolean isRecycle) throws Exception {
    StringBuffer sb = new StringBuffer();
    sb.append("var sce = new RSCE();\n");
    sb.append("sce.operator = RSCE.VALUE_OPERATOR_AND;\n");

    if (codeLike != null) {
      String condition = "\"" + alias + ".code like ?\"";
      String parameterValue = "\"" + codeLike + "\"";
      sb.append("sce.append(RSCE.cond(" + condition + ", " + parameterValue
          + ", RSCE.WC_RIGHT));\n");
    }
    if (nameLike != null) {
      String condition = "\"" + alias + ".name like ?\"";
      String parameterValue = "\"" + nameLike + "\"";
      sb
          .append("sce.append(RSCE.cond(" + condition + ", " + parameterValue
              + ", RSCE.WC_BOTH));\n");
    }
    if (!isRecycle.booleanValue()) {
      if (upperOrg != null) {
        String condition = "\"" + alias + ".upperOrganization.uuid = ?\"";
        String parameterValue = "\"" + upperOrg.getUuid() + "\"";
        sb.append("sce.append(RSCE.cond(" + condition + ", " + parameterValue + "));\n");
      } else {
        String condition = "\"" + alias + ".upperOrganization.uuid is null\"";
        sb.append("sce.append(RSCE.cond(" + condition + "));\n");
      }
      if (exceptOrg != null) {
        String condition = "\"" + alias + ".uuid != ?\"";
        String parameterValue = "\"" + exceptOrg.getUuid() + "\"";
        sb.append("sce.append(RSCE.cond(" + condition + ", " + parameterValue + "));\n");
      }
      if (VALUE_STATE_VALIDATE.equals(state)) {
        String condition = "\"" + alias + ".state != "
            + COrganization.VALUE_STATE_DELETED.toString() + "\"";
        sb.append("sce.append(RSCE.cond(" + condition + "));\n");
      }
    }

    sb.append("if (isDefined(sce.parameters))\n");
    sb.append("  " + qryName + ".setWhere(sce);\n");
    return sb.toString();
  }

  public String getCodeLike() {
    return codeLike;
  }

  public void setCodeLike(String codeLike) {
    this.codeLike = ("".equals(codeLike)) ? null : codeLike;
  }

  public String getNameLike() {
    return nameLike;
  }

  public void setNameLike(String nameLike) {
    this.nameLike = ("".equals(nameLike)) ? null : nameLike;
  }

  public QOrganization getUpperOrg() {
    return upperOrg;
  }

  public void setUpperOrg(QOrganization upperOrg) {
    this.upperOrg = upperOrg;
  }

  public Integer getState() {
    return state;
  }

  public void setState(Integer state) {
    this.state = state;
  }

  public QOrganization getExceptOrg() {
    return exceptOrg;
  }

  public void setExceptOrg(QOrganization exceptOrg) {
    this.exceptOrg = exceptOrg;
  }
}
